<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">

    <ui:composition template="layout.xhtml">
        <ui:define name="body">

            <h:form id="titleDocumentForm" enctype="multipart/form-data">
                <h1 class="title">Titulo e Documentos</h1>
                <p:growl id="messages" showDetail="true" autoUpdate="true"  /> 
                <div class="row">
                    <div class="twelve columns"> 
                        <fieldset>
                            <legend> <h4 style="margin:0 0 10px 0">Pesquisar</h4></legend>
                            <p:panel id="charterPanel" style="background: none; border: none;" 
                                     styleClass="container_24 clearfix ">


                                <p:outputLabel value="Número: " styleClass="grid_3"/>
                                <p:inputText styleClass="grid_8" value="#{managerSearchTitleDocument.titleDocument.register}"/>

                                <p:outputLabel value="Tipo: " styleClass="grid_4"/>
                                <div class="grid_8">
                                    <p:selectOneMenu style="width: 103%;" panelStyle="width:150px;" value="#{managerSearchTitleDocument.titleDocument.typeTitleDocument}">  
                                        <f:selectItem itemLabel="" itemValue="" />  
                                        <f:selectItems value="#{managerSearchTitleDocument.typeTitleDocumentList()}" />  
                                    </p:selectOneMenu> 
                                </div>
                                <div class="clear"/>

                                <p:outputLabel value="Protocolo: " styleClass="grid_3"/>
                                <p:inputText styleClass="grid_8" value="#{managerSearchTitleDocument.titleDocument.protocolo}"/>   

                                <p:outputLabel value="Livro Protocolo: " styleClass="grid_4"/>                      
                                <p:inputText styleClass="grid_8" value="#{managerSearchTitleDocument.titleDocument.bookprot}"/>   

                                <div class="clear"/>

                                <p:outputLabel value="Data Protocolo: " styleClass="grid_3"/>
                                <p:calendar showOn="button" styleClass="grid_8" style="width:32.55%"
                                            pattern="dd/MM/yyyy"
                                            value="#{managerSearchTitleDocument.titleDocument.datebp}"/>

                                <p:outputLabel value="Data Titulo: " styleClass="grid_4"/>
                                <p:calendar showOn="button" styleClass="grid_8" style="width:33%"
                                            pattern="dd/MM/yyyy"
                                            value="#{managerSearchTitleDocument.titleDocument.datetdoc}"/>


                                <div class="clear"/>

                                <p:outputLabel value="Livro: " styleClass="grid_3" />
                                <p:inputMask mask="?999999999" placeHolder="" styleClass="grid_8" value="#{managerSearchTitleDocument.titleDocument.book}"/>

                                <p:outputLabel value="Livro Compl: " styleClass="grid_4"/>
                                <p:inputText styleClass="grid_8" value="#{managerSearchTitleDocument.titleDocument.bookComp}"/> 

                                <div class="clear"/>

                                <p:outputLabel value="Folha: " styleClass="grid_3" />
                                <p:inputMask mask="?999999999" placeHolder="" styleClass="grid_8" value="#{managerSearchTitleDocument.titleDocument.leaf}"/> 

                                <p:outputLabel value="Folha Compl: " styleClass="grid_4"/>
                                <p:inputText styleClass="grid_8" value="#{managerSearchTitleDocument.titleDocument.leafComp}"/> 


                                <div class="clear"/>
                                <p:outputLabel value="Objeto: " styleClass="grid_3"/>
                                <p:inputTextarea cols="101" rows="4" maxlength="1000"
                                                 styleClass="grid_20" value="#{managerSearchTitleDocument.titleDocument.obj}"/> 

                                <div class="clear"/>
                                <p:outputLabel value="Observações: " styleClass="grid_3"/>
                                <p:inputTextarea cols="100" rows="4" maxlength="1000"
                                                 styleClass="grid_20" value="#{managerSearchTitleDocument.titleDocument.obs}"/> 
                            </p:panel>

                        </fieldset>
                        <fieldset>
                            <legend> <h4 style="margin:0 0 10px 0">Partes</h4></legend>

                            <p:panel id="partsPanel" style="background: none; border: none;" styleClass="container_24 clearfix ">

                                <p:outputLabel value="Pessoa: "  styleClass="grid_3"/>
                                <p:selectOneRadio id="typePerson" style="margin-top:-8px;" styleClass="grid_8"
                                                  value="#{managerSearchTitleDocument.people.tipoDocumento}" >  
                                    <f:selectItems value="#{managerSearchTitleDocument.typeDocumentList()}" />
                                    <p:ajax event="change" update=":titleDocumentForm:partsPanel" 
                                            listener="#{managerSearchTitleDocument.typeDocumentPeople}" />
                                </p:selectOneRadio>

                                <div class="clear"/>

                                <p:outputLabel id="typeDocumentLabel" 
                                               value="#{managerSearchTitleDocument.people.tipoDocumento}: " 
                                               styleClass="grid_3"/>

                                <p:inputMask id="typeDocumentInput" 
                                             value="#{managerSearchTitleDocument.people.tipoDocumento.type eq 'CPF' ? 
                                                      managerSearchTitleDocument.people.cpf : managerSearchTitleDocument.people.cnpj}"
                                             mask="#{managerSearchTitleDocument.people.tipoDocumento.type eq 'CPF' ? 
                                                     '999.999.999-99':'99.999.999/9999-99'}"
                                             styleClass="grid_8" > 
                                    <p:ajax event="blur" update=":titleDocumentForm:partsPanel" onstart="bui.show()" oncomplete="bui.hide()"
                                            listener="#{managerSearchTitleDocument.ajaxVerifyPeopleExist()}" />
                                </p:inputMask>

                                <p:blockUI block=":titleDocumentForm:partsPanel" widgetVar="bui"> 
                                    <p:graphicImage value="media/css/images/ajax-loader.gif"/>
                                </p:blockUI>

                                <div class="clear"/>

                                <p:outputLabel value="Nome: " styleClass="grid_3" />
                                <p:inputText styleClass="grid_20" value="#{managerSearchTitleDocument.people.name}"/> 

                                <div class="clear"/>

                                <p:outputLabel value="RG: " rendered="#{managerSearchTitleDocument.people.tipoDocumento.type eq 'CPF'}" styleClass="grid_3"/>
                                <p:inputText styleClass="grid_8" rendered="#{managerSearchTitleDocument.people.tipoDocumento.type eq 'CPF'}" value="#{managerSearchTitleDocument.people.rg}"/> 

                                <p:outputLabel value="Orgão emissor: " rendered="#{managerSearchTitleDocument.people.tipoDocumento.type eq 'CPF'}" styleClass="grid_4"/>
                                <p:inputText styleClass="grid_8" rendered="#{managerSearchTitleDocument.people.tipoDocumento.type eq 'CPF'}" value="#{managerSearchTitleDocument.people.organ}"/> 

                                <div class="clear"/>

                                <p:outputLabel value="Data nasc: " rendered="#{managerSearchTitleDocument.people.tipoDocumento.type eq 'CPF'}" styleClass="grid_3"/>
                                <p:calendar showOn="button" styleClass="grid_8" rendered="#{managerSearchTitleDocument.people.tipoDocumento.type eq 'CPF'}" style="width:32.44%" 
                                            value="#{managerSearchTitleDocument.people.nasc}"/> 

                                <p:outputLabel value="Qualidade: " 
                                               styleClass="#{managerSearchTitleDocument.people.tipoDocumento.type eq 'CPF' ? 'grid_4':'grid_3'}"/>
                                <p:autoComplete styleClass="grid_8" style="width: 28.44%" dropdown="true" converter="qualityconverter"   
                                                var="qualityValue" itemLabel="#{qualityValue.description}" scrollHeight="200"
                                                itemValue="#{qualityValue}" value="#{managerSearchTitleDocument.part.partQuality}" 
                                                completeMethod="#{managerSearchTitleDocument.autocompleteQuality}" />


                            </p:panel>
                        </fieldset>
                        <fieldset>
                            <legend> <h4 style="margin:0 0 10px 0">Selos</h4></legend>

                            <p:panel id="stampPanel" 
                                     style="background: none; border: none;" styleClass="container_24 clearfix ">

                                <p:outputLabel value="Série: "   styleClass="grid_3"/>
                                <p:inputText  styleClass="grid_8" value="#{managerSearchTitleDocument.stamp.serie}"/> 

                                <p:outputLabel value="Número: "  styleClass="grid_4"/>
                                <p:inputText  styleClass="grid_8" value="#{managerSearchTitleDocument.stamp.number}"/> 

                                <p:outputLabel value="Tipo Selo: "  styleClass="grid_3" />
                                <p:autoComplete styleClass="grid_8" style="width: 28.44%" dropdown="true"
                                                converter="typestampconverter"   forceSelection="true"
                                                value="#{managerSearchTitleDocument.typeStampObj}"
                                                var="typeStampValue" itemLabel="#{typeStampValue.name}" 
                                                itemValue="#{typeStampValue}"
                                                completeMethod="#{managerTypeStamp.autoComplete}" >

                                    <p:column style="width:20%;text-align:center;background-color: \##{typeStampValue.color};">  
                                    </p:column>  

                                    <p:column style="width:80%">  
                                        #{typeStampValue.name} 
                                    </p:column>
                                </p:autoComplete>


                            </p:panel>
                        </fieldset>
                        <br/>

                        <div class="buttonAction">
                            <p:commandButton id="searchBtn" value="Pesquisar" actionListener="#{managerSearchTitleDocument.search()}" 
                                             icon="ui-icon-search" styleClass="ui-priority-primary"
                                             update="@form, tdSearchTable" />
                            <p:commandButton value="Cancelar"  icon="ui-icon-trash" />
                        </div >
                        <h4 style="margin:0 0 10px 0">Resultado</h4>
                        <p:blockUI block="resultPanel" trigger="searchBtn" > 
                            <p:graphicImage value="media/css/images/ajax-loader.gif"/>
                        </p:blockUI>
                        <p:panel id="resultPanel" 
                                 style="background: none; border: none;" styleClass="container_24 clearfix ">
                            <p:dataTable var="cht" id="tdSearchTable" value="#{managerSearchTitleDocument.titleDocuments}"  
                                         style="white-space: pre-wrap;" selectionMode="single" rowKey="#{cht.id}"
                                         emptyMessage="Nenhum registro encontrado"
                                         paginatorPosition="bottom" paginator="true" rows="5"
                                         >
                                <f:facet name="header">
                                    Titulo e Documentos
                                </f:facet> 
                                <p:column headerText="Ação" style="width: 50px; vertical-align: middle;">  
                                    <p:button icon="ui-icon-search" href="showTitleDocument.xhtml?tdShow=#{cht.id}" style="width: 25px;"/>
                                    <p:button icon="ui-icon-pencil" href="editTitleDocument.xhtml?tdEdit=#{cht.id}" style="width: 25px;"/>
                                    <p:commandButton icon="ui-icon-trash" onclick="dlg3.show()"  style="width: 25px;"/>

                                </p:column> 
                                <p:column headerText="Nº" >  
                                    <h:outputLabel value="#{cht.register eq null ? '-':cht.register}" />  
                                </p:column>
                                <p:column headerText="Tipo" >  
                                    <h:outputLabel value="#{cht.typeTitleDocument.name}" />  
                                </p:column> 
                                <p:column headerText="Data" width="100" >  
                                    <h:outputLabel value="#{cht.datetdoc}" >
                                        <f:convertDateTime pattern="dd/MM/yyyy"/>
                                    </h:outputLabel>
                                </p:column> 
                                <p:column headerText="Partes" >  
                                    <p:dataTable var="part"   emptyMessage="Nenhuma parte encontrada" value="#{cht.parts}">
                                        <p:column headerText="Qualidade" width="20">  
                                            <h:outputLabel value="#{part.partQuality eq null ? '-': part.partQuality.description}" />
                                        </p:column> 
                                        <p:column headerText="Nome">  
                                            <h:outputLabel value="#{part.people.name}" />
                                        </p:column> 
                                        <p:column headerText="Doc">  
                                            <h:outputLabel value="#{part.people.cpf ne null ? part.people.cpf:part.people.cnpj}" />
                                        </p:column> 
                                    </p:dataTable>
                                </p:column> 
                            </p:dataTable>
                        </p:panel>
                    </div>
                </div>

                <p:dialog header="Exclusão de Registro" widgetVar="dlg3" modal="true"
                          showEffect="fade" hideEffect="fade" resizable="false" height="70">  
                    <h:outputText value="Deseja Excluir esse Registro ?" /> <br/> 
                    <f:facet name="footer">
                        <center>
                            <p:commandButton value="Sim" icon="ui-icon-check"
                                             actionListener="#{managerSearchTitleDocument.remove(cht)}" 
                                             update="@form"  />
                            <p:commandButton value="Não" icon="ui-icon-close"
                                             onclick="dlg3.hide();"  />
                        </center>
                    </f:facet>

                </p:dialog>
            </h:form>
        </ui:define>
    </ui:composition>

</html>
